<?php
namespace app\admin\controller;
class Work extends Common
{
    public function lst()
    {
        db('work')->where('pizhu_text|pizhu','<>','')->update(['status'=>1]);
        $aid = session('uid','','admin');
        $userInfo=db('admin')->where('id',$aid)->find();
        $group_id = $userInfo['group_id'];
        if($group_id == 1){
            $articleRes=db('work')->alias('w')->field('w.*,a.name as aname')->join('admin a','a.id=w.aid')->order('w.id desc')->paginate(10);
            $count=db('work')->count();
        }else{
            if($group_id == 5)
            {
                $groupMembers = $userInfo['group_members'];
                $groupMembers = $groupMembers?$groupMembers.','.$aid:$aid;
                $articleRes=db('work')->alias('w')->field('w.*,a.name as aname')->join('admin a','a.id=w.aid')->where('w.aid','IN',$groupMembers)->order('w.id desc')->paginate(10);
                $count=db('work')->where('aid',$aid)->count();
            }
            else
            {
                $articleRes=db('work')->alias('w')->field('w.*,a.name as aname')->join('admin a','a.id=w.aid')->where('w.aid',$aid)->order('w.id desc')->paginate(10);
                $count=db('work')->where('aid',$aid)->count();
            }

        }

        $this->assign([
            'articleRes'=>$articleRes,
            'count'=>$count,
        ]);
        return view();
    }

    public function add()
    {
        if(request()->isPost()){
            $data=input('post.')['data'];
            if(empty($data['id'])){
                $data['aid']=session('uid','','admin');
                $data['add_time']=time();
                $add=db('work')->insert($data);
            }else{
                $add=db('work')->where('id',$data['id'])->update($data);
            }
            
            if($add){
                return ['msg'=>'操作成功','icon'=>1];
            }else{
                return ['msg'=>'操作成功','icon'=>2];
            }
            return;
        }
        return view();
    }

    public function edit()
    {
        if(request()->isPost()){
            $data=input('post.')['data'];
            $save=db('work')->update($data);
            if($save !== false){
                return ['msg'=>'操作成功','icon'=>1];
            }else{
                return ['msg'=>'操作成功','icon'=>2];
            }
            return;
        }
        $art_id=input('id');
        $articles=db('work')->find($art_id);

        $this->assign([
            'articles'=>$articles
        ]);
        return view();
    }

    public function cont()
    {
        if(request()->isPost()){
            $data=input('post.')['data'];
            $save=db('work')->update($data);
            if($save !== false){
                return ['msg'=>'已阅读','icon'=>1];
            }else{
                return ['msg'=>'设为已阅读失败','icon'=>2];
            }
            return;
        }
        $art_id=input('id');
        $articles=db('work')->find($art_id);
        $aid = session('uid','','admin');
        $group_id=db('admin')->where('id',$aid)->value('group_id');
        $this->assign([
            'articles'=>$articles,
            'group_id'=>$group_id
        ]);
        return view();
    }
    
    
    public function del()
    {
        $id=input('id');
        //删除数据库记录
        $del=db('work')->delete($id);
        if($del){
            return ['msg'=>'删除总结成功','icon'=>1];
        }else{
            return ['msg'=>'删除总结失败','icon'=>2];
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    

}